<?php

class HmPoll_HmPollController extends Fw_Main
{
	public function addAction()
	{
		$this->validateUser();
		if($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
			if($formData) {
				$data = array(
							  'question' => $this->_request->getPost('question'),
							  'active' => $this->_request->getPost('active')
							  );
				$_model = $this->_getModel('HmPoll');
				$add_row = $_model->addRow($data);
				return $this->_helper->redirector('list');
			}
		}
	}
	
	public function editAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmPoll');
		$edit_poll = $_model->getRow($id);
		$this->view->edit_poll = $edit_poll;
		if($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
			if($formData) {
				$data = array(
							  'question' => $this->_request->getPost('question'),
							  'active' => $this->_request->getPost('active')
							  );
				$edit_row = $_model->updateRow($id, $data);
				return $this->_helper->redirector('list');
			}
		}
	}
	
	public function deleteAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmPoll');
		$delete_row = $_model->deleteRow($id);
		return $this->_helper->redirector('list');
	}
	
	public function answersAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmPollAnswer');
		$list_answer = $_model->getAll($id);
		$this->view->list_answer = $list_answer;
	}
	
	public function addAnswerAction()
	{
		if($this->_request->isPost()) {
			$this->validateUser();
			$formData = $this->_request->getPost('add');
			if($formData) {
				$id = (int)$this->_request->getParam('id');
				$data = array (
							   'answer'              => $this->_request->getPost('answer'),
							   'hm_poll_question_id' => $id
							   );
				$_model = $this->_getModel('HmPollAnswer');
				$add_row = $_model->addRow($data);
				$this->_redirector = $this->_helper->getHelper('Redirector');
				$this->_redirector->gotoSimple('answers','hm-poll','hm-poll',array('id' => $id));
        		return;
			}
		}
	}
	
	public function deleteAnswerAction()
	{
		$this->validateUser();
		$id_question = (int)$this->_request->getParam('id');
		$id_answer = (int)$this->_request->getParam('answer_id');
		$_model = $this->_getModel('HmPollAnswer');
		$delete_row = $_model->deleteRow($id_answer);
		$this->_redirector = $this->_helper->getHelper('Redirector');
		$this->_redirector->gotoSimple('answers','hm-poll','hm-poll',array('id' => $id_question));
		return;
	}
	
	public function listAction()
	{
		$this->validateUser();
		$_model = $this->_getModel('HmPoll');
		$list_poll = $_model->getAll();
		$this->view->list_poll = $list_poll;
		$this->view->rowcount  = count($list_poll);
	}
	
	public function randAction()
	{
		$_model = $this->_getModel('HmPoll');
		$list_poll = $_model->getLast('1');
		$this->view->list_poll = $list_poll;
		$this->view->rowcount  = count($list_poll);
		foreach($list_poll as $list_poll) {
			$id = $list_poll['id'];
		}
		$_newModel = $this->_getNewModel('HmPollAnswer');
		$list_answer = $_newModel->getAll($id);
		$this->view->list_answer = $list_answer;
	}
	
	public function resultAction()
	{
		$id = $this->_request->getParam('id');
		
		$_model = $this->_getModel('HmPoll');
		$list_poll = $_model->getRow($id);
		$this->view->list_poll = $list_poll;
		
		$_newModel = $this->_getNewModel('HmPollAnswer');
		$list_answer = $_newModel->getAll($id);
		$this->view->list_answer = $list_answer;
	}
	
	public function voteAction()
	{
		if($this->_request->isPost()) {
			$formData = $this->_request->getPost('vote');
			if($formData) {
				$id_question = $this->_request->getPost('id_question');
				$id_answer   = $this->_request->getPost('id_answer');
				$_model = $this->_getModel('HmPollAnswer');
				$edit_row = $_model->assignVote($id_answer);
				$this->_redirector = $this->_helper->getHelper('Redirector');
				$this->_redirector->gotoSimple('result','hm-poll','hm-poll',array('id' => $id_question));
				return;
			}
		}
	}
}